(function() {
	String.prototype.repeat = function(count) {
		if (count < 1)
			return '';
		var result = '', pattern = this.valueOf();
		while (count > 0) {
			if (count & 1)
				result += pattern;
			count >>= 1, pattern += pattern;
		}
		return result;
	};
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g, "");
	}
	String.prototype.splice = function( idx, rem, s ) {
    	return (this.slice(0,idx) + s + this.slice(idx + Math.abs(rem)));
	};

})();

var util = {
	id : 0,
	ic:{},
	rc:[],
	uc:[],
	call : function(url, opts) {
		//$('<script>').attr('src',url).appendTo('#scripts'); //IE bug.
		util.id++;
		var id = 'script' + new Date().getTime();
		//+util.id;
		var s = document.getElementById(id);
		var head = document.getElementsByTagName('head').item(0);
		s = document.createElement('script');
		s.id = id;
		s.type = 'text/javascript';
		s.charset = 'UTF-8';
		s.async = true;
		s.src = url;
		head.appendChild(s);
		setTimeout(
			function(){
				if(document.getElementById(id)){					
					s.parentNode.removeChild(s);
					if(opts && opts.timeout) opts.timeout();
				}
			}
			, 5000
		)
		s.onreadystatechange = function() {
			if (this.readyState == 'complete' || this.readyState == 'loaded') {
				if (s && s.parentNode)
					s.parentNode.removeChild(s);
				if (opts && opts.complete)
					complete();
			}
		}
		s.onload = function() {
			if (s && s.parentNode)
				s.parentNode.removeChild(s);
			if (opts && opts.complete)
				opts.complete();
		}
	},
	encodeQuery : function(v) {
		return encodeURIComponent(v + ' ').replace(/%/g, '`');
	},
	alert:function(content, el){
		var html = _.template($("#alert_template").html(), {c: content});
		var box = $(html);
		el.html(box)
		return box;
	},
	getUrl: function(block){
		var u = url[block];
		for(var i =1; i<arguments.length; i++){
			u = u.replace(i-1, arguments[i]);
		}
		return url.base + u;				
	},
	getBlock:function(name){
		return $("[name='"+name+"'].panel-container");
	},
	login: function(usr, pwd, env){
		if(!user_data[usr])
			ViewData.Login = {
				err:"Username & Password didn't match."
			}
		else{
			ViewData.Login = user_data[usr];			
		}
		
	}
	
}

var Auth = {
	ctrl: []
};
